1 Spring Bean 装配之Bean的定义及其作用域的注解实现







< context:component-scan>




例子
1 2 3
| <context:component-scan base-package="bean.BeanAnnotation">
<context:component-scan/>
|
1 2 3 4 5 6 7
| package bean; @Component public class BeanAnnotation { public void say(){ System,out.print("Hello Spring"); } }
|
1 2 3 4
| @Test BeanAnnotation bean=super.getBean("beanAnnotation"); bean.say("This is Tese"); }
|



2 Spring装配 基于java容器的注解说明 @importResource和@Value


1 2 3 4 5 6 7 8 9
| public class MyDriverManager {
public MyDriverManager(String url, String userName, String password) { System.out.print(url); System.out.print(userName); System.out.print(password);
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Configuration @ImportResource("classpath:config.xml") public class StoreConfig {
@Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password;
@Bean public MyDriverManager myDriverManager() { return new MyDriverManager(url, username, password); }
}
|
1 2 3 4 5 6 7 8
| public class Test {
@Test public void testMyDreiverManager() { MyDriverManager myDriverManager = super.getBean("myDriverManager"); myDriverManager.getClass().getName(); } }
|




3@Bean 和@Scope



4基于泛型的自动装配

Aware 和Autowiring





Author:
John Doe
Permalink:
http://yoursite.com/2018/12/10/Spring/4.SpringBean 装配/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?